<template>
  <div class="app-container">
    <el-card class="box-card">
      <div
        slot="header"
        class="clearfix"
      >
        <span>求职简历管理</span>
      </div>
      <div class="list-search">
        <el-select
          v-model="form.is_end"
          class="list-options"
          placeholder="过期状态"
          @change="funSearch"
        >
          <el-option label="不限过期状态" value="-1" />
          <el-option label="未过期" value="0" />
          <el-option label="已过期" value="1" />
        </el-select>
        <el-select
          v-model="form.audit"
          class="list-options"
          placeholder="审核状态"
          @change="funSearch"
        >
          <el-option label="不限审核状态" value="-1" />
          <el-option label="未审核" value="0" />
          <el-option label="已通过" value="1" />
          <el-option label="未通过" value="2" />
        </el-select>
        <el-select
          v-model="form.is_top"
          class="list-options"
          placeholder="置顶状态"
          @change="funSearch"
        >
          <el-option label="不限置顶状态" value="-1" />
          <el-option label="未置顶" value="0" />
          <el-option label="已置顶" value="1" />
        </el-select>
        <el-select
          v-model="form.is_recommend"
          class="list-options"
          placeholder="推荐状态"
          @change="funSearch"
        >
          <el-option label="不限推荐状态" value="-1" />
          <el-option label="未推荐" value="0" />
          <el-option label="已推荐" value="1" />
        </el-select>

        <el-input
          v-model="keyword"
          placeholder="请输入搜索内容"
          class="input-with-select"
          @keyup.enter.native="funSearchKeyword"
        >
          <el-select
            slot="prepend"
            v-model="key_type"
            placeholder="请选择"
            class="input-sel"
          >
            <el-option
              label="姓名"
              value="1"
            />
            <el-option
              label="期望职位"
              value="2"
            />
            <el-option
              label="具体描述"
              value="3"
            />
            <el-option
              label="手机号"
              value="4"
            />
          </el-select>
          <el-button
            slot="append"
            icon="el-icon-search"
            @click="funSearchKeyword"
          />
        </el-input>
      </div>
      <div class="spaceline" />
      <el-table
        v-loading="listLoading"
        :data="list"
        element-loading-text="Loading"
        fit
        highlight-current-row
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          type="selection"
          width="42"
        />

        <el-table-column label="基本信息" min-width="150">
          <template slot-scope="scope">
            <div class="namecol">
              <span>
                <div>
                  {{ scope.row.fullname }} · <span>{{ scope.row.sex_text }}</span>
                  ·
                  <span>{{ scope.row.experience_text }}</span>
                </div>
                <div>
                  {{ scope.row.telephone }}

                </div>
              </span>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="想找工作" min-width="220">
          <template slot-scope="scope">
            <div class="namecol">
              <span>
                <div class="substsring" style="width: 220px">
                  {{ scope.row.wantjob }}
                </div>
                <el-tooltip class="item" effect="dark" :content="scope.row.content_" placement="top">
                  <div class="substsring" style="width: 220px;">
                    {{ scope.row.content_ }}
                  </div>
                </el-tooltip>
              </span>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          label="审核状态"
          show-overflow-tooltip
          min-width="100"
        >
          <template slot-scope="scope">
            <el-tag :type="scope.row.audit | auditFilter">
              {{ scope.row.audit_text }}
            </el-tag>
          </template>
        </el-table-column>

        <el-table-column
          label="置顶"
          show-overflow-tooltip
          min-width="70"
        >
          <template slot-scope="scope">
            <el-tag :type="scope.row.is_top | auditFilter">
              {{ scope.row.top_text }}
            </el-tag>
          </template>
        </el-table-column>

        <el-table-column
          label="推荐"
          show-overflow-tooltip
          min-width="70"
        >
          <template slot-scope="scope">
            <el-tag :type="scope.row.is_recommend | auditFilter">
              {{ scope.row.recommend_text }}
            </el-tag>
          </template>
        </el-table-column>

        <el-table-column label="添加 / 到期时间" min-width="160">
          <template slot-scope="scope">
            <div class="namecol">
              <span>
                <div title="添加时间">
                  <i class="el-icon-time" />{{ scope.row.addtime_cn }}
                </div>
                <div title="到期时间">
                  <i class="el-icon-time" />{{ scope.row.endtime_cn }}
                </div>
              </span>
            </div>
          </template>
        </el-table-column>

        <el-table-column
          label="刷新时间"
          show-overflow-tooltip
          min-width="160"
        >
          <template slot-scope="scope">
            <i class="el-icon-time" />{{ scope.row.refreshtime_cn }}
          </template>
        </el-table-column>
        <el-table-column
          fixed="right"
          label="操作"
          min-width="280"
        >
          <template slot-scope="scope">
            <el-button
              size="small"
              type="primary"
              @click="funEdit(scope.$index, scope.row)"
            >
              编辑
            </el-button>
            <el-button
              size="small"
              type="primary"
              @click="funValid(scope.$index, scope.row)"
            >
              延期
            </el-button>
            <el-button
              size="small"
              type="primary"
              @click="funEditPwd(scope.$index, scope.row)"
            >
              改密
            </el-button>
            <el-button
              size="small"
              type="danger"
              @click="funDelete(scope.$index, scope.row)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="spaceline" />
      <el-row :gutter="20">
        <el-col :span="8">
          <el-button
            size="small"
            type="primary"
            @click="goAdd"
          >
            新增
          </el-button>
          <el-button
            size="small"
            type="warning"
            @click="goAudit"
          >
            审核
          </el-button>
          <el-button
            size="small"
            type="primary"
            @click="goTop"
          >
            置顶
          </el-button>
          <el-button
            size="small"
            type="primary"
            @click="goRecommend"
          >
            推荐
          </el-button>
          <el-button
            size="small"
            type="primary"
            @click="goRefresh"
          >
            刷新
          </el-button>
          <el-button
            size="small"
            type="danger"
            @click="goDelete"
          >
            删除所选
          </el-button>
        </el-col>
        <el-col
          :span="16"
          style="text-align: right;"
        >
          <el-pagination
            background
            :current-page="currentPage"
            :page-sizes="[10, 20, 50, 100]"
            :page-size="pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </el-col>
      </el-row>
    </el-card>

    <el-dialog
      :visible.sync="dialogFormVisibleValid"
      title="将所选信息延期"
      width="25%"
      style="padding-top:30px;"
    >
      <el-form class="common-form" label-width="100px">
        <el-form-item label="有效期截止">
          {{ validendtime }}
        </el-form-item>
        <el-form-item label="有效期延长" prop="valid" style="margin-bottom:0;">
          <el-input v-model="setValidVal" style="width:230px;margin-right:10px;" />天
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisibleValid = false">取 消</el-button>
        <el-button type="primary" @click="doSetValid">
          确 定
        </el-button>
      </div>
    </el-dialog>
    <el-dialog
      :visible.sync="dialogFormVisibleAudit"
      title="将所选信息设置为"
      width="25%"
    >
      <el-form class="common-form" label-width="80px">
        <el-form-item label="审核状态">
          <el-radio-group v-model="setAuditVal">
            <el-radio label="0">待审核</el-radio>
            <el-radio label="1">已通过</el-radio>
            <el-radio label="2">未通过</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item v-if="setAuditVal == 2" label="原因">
          <el-input v-model="setAuditReason" rows="3" type="textarea" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisibleAudit = false">取 消</el-button>
        <el-button type="primary" @click="doSetAudit">
          确 定
        </el-button>
      </div>
    </el-dialog>

    <el-dialog
      :visible.sync="dialogFormVisibleEditPwd"
      title="修改管理密码为"
      width="25%"
    >
      <el-form class="common-form" label-width="80px">
        <el-form-item label="管理密码">
          <el-input v-model="setEditPwdVal" type="text" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisibleEditPwd = false">取 消</el-button>
        <el-button type="primary" @click="doSetEditPwd">
          确 定
        </el-button>
      </div>
    </el-dialog>
    <el-dialog
      title="将所选信息设置为推荐"
      :visible.sync="dialogFormVisibleRecommend"
      width="25%"
    >
      <el-form class="common-form" label-width="80px">
        <el-form-item label="推荐状态">
          <el-radio-group v-model="setRecommendVal">
            <el-radio label="0">不推荐</el-radio>
            <el-radio label="1">推荐</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisibleRecommend = false">取 消</el-button>
        <el-button type="primary" @click="doSetRecommend">
          确 定
        </el-button>
      </div>
    </el-dialog>

    <el-dialog
      title="将所选信息设置为置顶"
      :visible.sync="dialogFormVisibleTop"
      width="25%"
    >
      <el-form class="common-form" label-width="80px">
        <el-form-item label="置顶状态">
          <el-radio-group v-model="setTopVal">
            <el-radio label="0">不置顶</el-radio>
            <el-radio label="1">置顶</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisibleTop = false">取 消</el-button>
        <el-button type="primary" @click="doSetTop">
          确 定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { parseTime } from '@/utils'
import { fastResumeList, fastResumeEdit, fastResumeAudit, fastResumeTop, fastResumeRec, fastResumeRefesh, fastResumeValid, fastResumeDelete, fastResumeEditPwd, fastResumeDeleteAll } from '@/api/fast_resume'

export default {
  filters: {
    displayFilter(is_display) {
      const displayMap = {
        1: 'success',
        0: 'info'
      }
      return displayMap[is_display]
    },
    timeFilter(timestamp) {
      return parseTime(timestamp, '{y}-{m}-{d} {h}:{i}')
    },
    auditFilter(audit) {
      switch (audit) {
        case 0:
          return 'warning'
        case 1:
          return 'success'
        case 2:
          return 'danger'
        default:
          return ''
      }
    }
  },
  data () {
    return {
      form: {
        audit: '-1',
        is_end: '-1',
        is_top: '-1',
        is_recommend: '-1',
        key_type: '1',
        keyword: '',
        page: 1,
        pagesize: 10
      },
      validendtime: '',
      optionid: 0,
      tableIdsArr: [],
      list: null,
      listLoading: true,
      display: '',
      total: 0,
      currentPage: 1,
      pagesize: 10,
      is_display: '',
      timecase: '',
      orderby: '',
      adminpwd: '',
      key_type: '1',
      setEditPwdVal: '',
      dialogFormVisibleEditPwd: false,
      editPwdSubmitLoading: false,
      setValidVal: '0',
      dialogFormVisibleValid: false,
      validSubmitLoading: false,
      setAuditReason: '',
      setAuditVal: '0',
      dialogFormVisibleAudit: false,
      auditSubmitLoading: false,
      setRecommendVal: '0',
      dialogFormVisibleRecommend: false,
      recommendSubmitLoading: false,
      setTopVal: '0',
      dialogFormVisibleTop: false,
      topSubmitLoading: false,
      keyword: ''
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData () {
      this.listLoading = true
      const param = {
        audit: this.form.audit,
        is_end: this.form.is_end,
        is_top: this.form.is_top,
        is_recommend: this.form.is_recommend,
        keyword: this.keyword,
        key_type: this.key_type,
        page: this.currentPage,
        pagesize: this.pagesize
      }
      fastResumeList(param).then(response => {
        this.list = response.data.items
        this.listLoading = false
        this.total = response.data.total
        this.currentPage = response.data.current_page
        this.pagesize = response.data.pagesize
      })
    },
    funAudit(row) {
      this.setAuditReason = ''
      this.setAuditVal = row.audit
      this.auditIdarr = [row.id]
      this.dialogFormVisible = true
    },
    funAuditBatch() {
      if (this.tableIdarr.length == 0) {
        this.$message.error('请选择要审核的简历')
        return false
      }
      this.auditIdarr = this.tableIdarr
      this.setAuditVal = '0'
      this.setAuditReason = ''
      this.dialogFormVisible = true
    },
    funSearch() {
      this.fetchData()
    },
    funSearchKeyword() {
      this.currentPage = 1
      this.fetchData()
    },
    handleSizeChange(val) {
      this.pagesize = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.fetchData()
    },
    handleSelectionChange (idList) {
      this.tableIdsArr = []
      if (idList.length > 0) {
        for (const item of idList) {
          this.tableIdsArr.push(item.id)
        }
      }
    },
    funEdit (index, row) {
      this.$router.push({
        path: '/fast/resume/edit',
        query: {
          id: row.id
        }
      })
    },
    funEditPwd (index, row) {
      this.optionid = row.id
      this.setEditPwdVal = ''
      this.dialogFormVisibleEditPwd = true
    },
    doSetEditPwd(){
      if (this.editPwdSubmitLoading){
        return false
      }
      this.editPwdSubmitLoading = true
      const params = {
        id: this.optionid,
        adminpwd: this.setEditPwdVal
      }
      fastResumeEditPwd(params, 'post').then(response => {
        if (response.code == 200) {
          this.$message.success(response.message)
          this.editPwdSubmitLoading = false
          this.fetchData()
          this.dialogFormVisibleEditPwd = false
          return true
        } else {
          this.editPwdSubmitLoading = false
          this.$message.error(response.message)
          return false
        }
      })
    },
    funValid (index, row) {
      fastResumeEdit({ id: row.id }, 'get').then(respons => {
        this.validendtime = respons.data.info.endtime_cn
      })
      this.optionid = row.id
      this.setValidVal = '0'
      this.dialogFormVisibleValid = true
    },
    doSetValid(){
      if (this.validSubmitLoading){
        return false
      }
      this.validSubmitLoading = true
      const params = {
        id: this.optionid,
        valid: this.setValidVal
      }
      fastResumeValid(params, 'post').then(response => {
        if (response.code == 200) {
          this.$message.success(response.message)
          this.validSubmitLoading = false
          this.fetchData()
          this.dialogFormVisibleValid = false
          return true
        } else {
          this.validSubmitLoading = false
          this.$message.error(response.message)
          return false
        }
      })
    },
    funDelete (index, row) {
      var that = this
      that
        .$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        .then(() => {
          const param = {
            id: row.id
          }
          fastResumeDelete(param).then(response => {
            that.$message.success(response.message)
            that.fetchData()
            return true
          })
        })
        .catch(() => {})
    },
    goAdd() {
      this.$router.push('/fast/resume/add')
    },
    goAudit() {
      if (this.tableIdsArr.length == 0) {
        this.$message.error('请选择要审核的简历')
        return false
      }
      this.setAuditVal = '0'
      this.setAuditReason = ''
      this.dialogFormVisibleAudit = true
    },
    doSetAudit(){
      if (this.auditSubmitLoading == true) {
        return false
      }
      this.auditSubmitLoading = true
      const params = {
        id: this.tableIdsArr,
        audit: this.setAuditVal,
        reason: this.setAuditReason
      }
      fastResumeAudit(params, 'post').then(response => {
        if (response.code == 200) {
          this.$message.success(response.message)
          this.auditSubmitLoading = false
          this.fetchData()
          this.dialogFormVisibleAudit = false
          return true
        } else {
          this.auditSubmitLoading = false
          this.$message.error(response.message)
          return false
        }
      })
    },
    goTop() {
      if (this.tableIdsArr.length == 0) {
        return this.$message.error('请选择要置顶的简历')
      }
      this.setTopVal = '0'
      this.dialogFormVisibleTop = true
    },
    doSetTop(){
      if (this.topSubmitLoading){
        return false
      }
      this.topSubmitLoading = true
      const params = {
        id: this.tableIdsArr,
        top: this.setTopVal
      }
      fastResumeTop(params, 'post').then(response => {
        if (response.code == 200) {
          this.$message.success(response.message)
          this.topSubmitLoading = false
          this.fetchData()
          this.dialogFormVisibleTop = false
          return true
        } else {
          this.topSubmitLoading = false
          this.$message.error(response.message)
          return false
        }
      })
    },
    goRecommend() {
      if (this.tableIdsArr.length == 0) {
        return this.$message.error('请选择要推荐的简历')
      }
      this.setRecommendVal = '0'
      this.dialogFormVisibleRecommend = true
    },
    doSetRecommend(){
      if (this.recommendSubmitLoading){
        return false
      }
      this.recommendSubmitLoading = true
      const params = {
        id: this.tableIdsArr,
        recommend: this.setRecommendVal
      }
      fastResumeRec(params, 'post').then(response => {
        if (response.code == 200) {
          this.$message.success(response.message)
          this.recommendSubmitLoading = false
          this.fetchData()
          this.dialogFormVisibleRecommend = false
          return true
        } else {
          this.recommendSubmitLoading = false
          this.$message.error(response.message)
          return false
        }
      })
    },
    goRefresh() {
      var that = this
      if (that.tableIdsArr.length === 0) {
        that.$message.error('请选择要刷新的简历')
        return false
      }
      that
        .$confirm('此操作将刷新选中的简历, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        .then(() => {
          const param = {
            id: that.tableIdsArr
          }
          fastResumeRefesh(param).then(response => {
            that.$message.success(response.message)
            that.fetchData()
            return true
          })
        })
        .catch(() => {})
    },
    goDelete() {
      var that = this
      if (that.tableIdsArr.length === 0) {
        that.$message.error('请选择要删除的简历')
        return false
      }
      that
        .$confirm('此操作将永久删除选中的简历, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        .then(() => {
          const param = {
            id: that.tableIdsArr
          }
          console.log(param)
          fastResumeDeleteAll(param).then(response => {
            that.$message.success(response.message)
            that.fetchData()
            return true
          })
        })
        .catch(() => {})
    }
  }
}
</script>
<style>
.el-dialog__body{
  padding-top:30px;
}
.substsring{
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
</style>
